package com.hospital.xml;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;


public class HosHandler extends DefaultHandler {
	
	private String preTAG;
	private List<HashMap<String,String>> ListHos;  
	private HashMap<String,String> taglist;
    private HashMap<String,String> hos; 
    private HashMap<String, String> info;
    private Set<String> set;
    private String[] infolist={"ret_code","ret_info","total_count","current_count"};
    
    public HosHandler(List<HashMap<String,String>> listHos){
    	super();
    	ListHos=listHos;
    	taglist=ListHos.get(0);
    	set=taglist.keySet();//zz
    	ListHos.clear();
    }
    @Override
    public void startDocument() throws SAXException {
    	  // TODO Auto-generated method stub
    	info=new HashMap<String, String>();
    	for(String tmp:infolist)
    		info.put(tmp, "");
    	  super.startDocument();
    	}
    
    
    @Override
	public void startElement(String uri, String localName, String qName,  
            Attributes attributes) throws SAXException{
    	
    	
    	preTAG=localName;
    	if(taglist.get("elementname").equals(localName))
    	{
    		hos=new HashMap<String,String>();
    		for(String tmp:set)
    		{
    			
    			hos.put(tmp, "");
    		}
    	}
    	super.startElement(uri, localName, qName, attributes);
    	
    	
    }

    
    
    @Override
	public void endElement(String uri, String localName, String qName)  
            throws SAXException {
    	preTAG=""; 
    	if(taglist.get("elementname").equals(localName))
    		ListHos.add(hos);
    	
    	
    	super.endElement(uri, localName, qName);
    	
    }
    
    
    @Override
	public void characters(char[] ch, int start, int length)  
            throws SAXException {
    	
    	String dateString; 
    	

    	if (taglist.containsKey(preTAG)){
    	
    		dateString=new String(ch,start,length);    
            hos.put(preTAG,hos.get(preTAG)+dateString);
    	}else if(info.containsKey(preTAG)){
    		dateString=new String(ch,start,length);
            info.put(preTAG,info.get(preTAG)+dateString);
    	}
    	/*
    	if("hospital_name".equals(preTAG)){
    		dateString=new String(ch,start,length);
    		hos.setHospital_name(hos.getHospital_name()+dateString);
    	}
    	if("hospital_introduction".equals(preTAG)){
    		dateString=new String(ch,start,length);
    		hos.setHospital_introduction(hos.getHospital_introduction()+dateString);
    	}
    	if("hospital_picture".equals(preTAG)){
    		dateString=new String(ch,start,length);
    		hos.setHospital_picture(hos.getHospital_picture()+dateString);
    	}*/
    	
   	super.characters(ch, start, length);
    }
    
    @Override
    public void endDocument() throws SAXException {
    	  // TODO Auto-generated method stub
    	ListHos.add(info);
    	  super.endDocument();
    	}
}
